//
//  PlayerDataBase.m
//  WXMusic
//
//  Created by 漫步人生路 on 15/9/9.
//  Copyright (c) 2015年 漫步人生路. All rights reserved.
//

#import "PlayerDataBase.h"
@implementation PlayerDataBase
static PlayerDataBase *playerDataBase;
+ (PlayerDataBase *)shareDataBase
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        playerDataBase = [[PlayerDataBase alloc]init];
    });
    return playerDataBase;
}
//创建fmdb
- (FMDatabase *)dataBase
{
    if (!_dataBase) {
        _dataBase = [[FMDatabase alloc]init];
    }
    return _dataBase;
}

//指定数据库路径
- (NSString *)sqlitePaths:(NSString *)sqlitePaths
{
    NSString *str = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:sqlitePaths];
    NSLog(@"%@",str);
    return str;
}
//打开数据库
- (void)openSqliteWithPaths:(NSString *)paths
{

    self.dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:paths]];

}
//关闭数据
- (void)closeSqlite
{
    [self.dataBase close];
}
//创建数据库表
- (void)createPlaySongListWithName:(NSString *)name
{
    if (!_dataBase) {
        [self openSqliteWithPaths:[NSString stringWithFormat:@"%@.db",name]];
    }
    if ([_dataBase open]) {
        BOOL executeUpdate = [_dataBase executeUpdate:@"CREATE TABLE SongList(ID Integer PRIMARY KEY,SongName TEXT ,SingerName TEXT,SongImage TEXT,Lrcy TEXT,SongUrl TEXT)"];
        if (executeUpdate) {
            NSLog(@"建表成功");
        }else
        {
            NSLog(@"建表失败");
        }
    }

}
- (void)insertModel:(PlayModel *)model
{
    if (!model) {
        NSLog(@"model为空");
        return;
    }
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"insert into SongList(ID,SongName,SingerName,SongImage,Lrcy,SongUrl) values(?,?,?,?,?,?)"];
        BOOL insertModel = [_dataBase executeUpdate:sql,model.song_id,model.title,model.author,model.pic_huge,model.lrclink,model.file_link];
        
        if (insertModel) {
            NSLog(@"插入成功");
        }else
        {
            NSLog(@"插入失败");
        }
    }
}

- (NSMutableArray *)selegetAll
{
    NSMutableArray *model = [[NSMutableArray alloc]init];
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = @"select * from SongList";
        FMResultSet *rs = [_dataBase executeQuery:sql];
        while ([rs next]) {
            PlayModel *playModel = [[PlayModel alloc]init];
            playModel.title = [rs stringForColumn:@"SzongName"];
            playModel.author = [rs stringForColumn:@"SingerName"];
            playModel.lrclink = [rs stringForColumn:@"Lrcy"];
            playModel.pic_huge = [rs stringForColumn:@"SongImae"];
            playModel.file_link = [rs stringForColumn:@"SongUrl"];
            [model addObject:playModel];
        }
    }
    return model;
}
- (void)deleteCinemaModelWithSongID:(NSInteger)songID
{
    if (!_dataBase) {
        _dataBase = [FMDatabase databaseWithPath:[self sqlitePaths:@"SongList.db"]];
    }
    if ([_dataBase open]) {
        NSString *sql = [NSString stringWithFormat:@"delete from SongList where ID = ?"];
        BOOL b = [_dataBase executeUpdate:sql,[NSString stringWithFormat:@"%ld",songID]];
        if (b) {
            NSLog(@"删除成功");
        }else{
            NSLog(@"删除失败");
        }

    }
}
@end
